//
//  SYP_LeftViewController.m
//  wind
//
//  Created by SYP on 16/1/14.
//  Copyright © 2016年 李金明. All rights reserved.
//

#define HEADHEIGHT (100) // 用户信息的高度

#import "SYP_LeftViewController.h"
#import "SYP_DrawerHeadView.h" // 用户信息(头)
#import "Masonry.h"

@interface SYP_LeftViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *functionTableV;
@property (nonatomic, retain) NSMutableArray *picArr;
@property (nonatomic, retain) NSMutableArray *nameArr;
@property (nonatomic, retain) UISwitch *swith;

@end

@implementation SYP_LeftViewController

- (void)dealloc
{
    [_swith release];
    [_picArr release];
    [_nameArr release];
    [_functionTableV release];
    [super dealloc];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:0.415 green:0.581 blue:1.000 alpha:0.500];
    
    [self creatHeadView];
    [self creatFunctionListView];
}


#pragma 创建用户信息View(头)
- (void)creatHeadView
{
    SYP_DrawerHeadView *headV = [[SYP_DrawerHeadView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, SCREEN_HEIGHT / 6)];
    headV.backgroundColor = [UIColor colorWithRed:0.386 green:0.407 blue:1.000 alpha:0.496];
    [self.view addSubview:headV];
    [headV release];
    
}

#pragma 创建功能列表
- (void)creatFunctionListView
{
    self.picArr = [NSMutableArray arrayWithObjects:@"夜间模式", @"扫描音乐", @"清理缓存", @"设置", @"关于我们", nil];
    self.nameArr = [NSMutableArray arrayWithObjects:@"夜间模式", @"扫描音乐", @"清理缓存", @"设置", @"关于我们", nil];
    self.functionTableV = [[UITableView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT / 6, self.view.frame.size.width, SCREEN_HEIGHT / 6 * 5) style:UITableViewStyleGrouped];
    self.functionTableV.bounces = NO;
    self.functionTableV.backgroundColor = [UIColor clearColor];
    self.functionTableV.delegate = self;
    self.functionTableV.dataSource = self;
    [self.view addSubview:self.functionTableV];
    [_functionTableV release];
}

// 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

// 创建cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.swith = [[UISwitch alloc] initWithFrame:CGRectZero];
    self.swith.tintColor = [UIColor colorWithRed:0.227 green:0.659 blue:1.000 alpha:0.496];
    self.swith.onTintColor = [UIColor colorWithRed:0.567 green:0.549 blue:1.000 alpha:0.506];
    [self.swith addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    
    // 设置开关初始状态
    self.swith.on = [[NSUserDefaults standardUserDefaults] boolForKey:@"isNight"];
    static NSString *cellStr = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    if (nil == cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr] autorelease];
    }
    cell.imageView.image = [UIImage imageNamed:self.picArr[indexPath.row]];
    cell.textLabel.text = self.nameArr[indexPath.row];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (0 == indexPath.row) {
        
        self.swith.frame = CGRectMake(cell.contentView.frame.size.width / 5 * 3, 7, cell.contentView.frame.size.width / 6, 15);
        [cell.contentView addSubview:self.swith];
        
    }
    return cell;
    
    
}

#pragma 夜间模式按钮
- (void)switchAction:(UISwitch *)sender
{
    [[NSUserDefaults standardUserDefaults] setBool:sender.on forKey:@"isNight"];
    if (sender.on) {
        NSLog(@"选中, 夜间模式开启");
        [[NSNotificationCenter defaultCenter] postNotificationName:@"nightState" object:nil];
        
    } else {
        NSLog(@"未选中, 夜间模式关闭");
        [[NSNotificationCenter defaultCenter] postNotificationName:@"dayState" object:nil];
        
    }
}

#pragma 抽屉的内容实现
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (1 == indexPath.row) {

    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
